package com.itheima.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.itheima.reggie.common.UserHolder;
import com.itheima.reggie.domain.Cart;
import com.itheima.reggie.mapper.CartMapper;
import com.itheima.reggie.mapper.DishMapper;
import com.itheima.reggie.service.CartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;


// 购物车
@Service
@Transactional
public class CartServiceImpl implements CartService {
    @Autowired
    private CartMapper cartMapper;

    //添加购物车
    @Override
    public Cart add(Cart cartParam) {
        //先查购物车里是否有数据
        //构建条件
        LambdaQueryWrapper<Cart> qw = new LambdaQueryWrapper<>();
        //用户id   UserHolder.get()====操作工具类
        qw.eq(Cart::getUserId, UserHolder.get().getId());
        //菜品id  动态sql
        qw.eq(cartParam.getDishId() != null, Cart::getDishId, cartParam.getDishId());
        //套餐id 动态sql
        qw.eq(cartParam.getSetmealId() != null, Cart::getSetmealId, cartParam.getSetmealId());
        Cart cart = cartMapper.selectOne(qw);
        //判断
        if (cart == null) {
            //补全用户id
            cartParam.setUserId(UserHolder.get().getId());
            //补全数量
            cartParam.setNumber(1);
            //手动填写时间
            cartParam.setCreateTime(new Date());
            cartMapper.insert(cartParam);
            cart = cartParam;
        } else {//存在购买记录，购买数量加1
            cart.setNumber(cart.getNumber() + 1);
            cartMapper.updateById(cart);
        }
        //返回购物车
        return cart;
    }
//查询菜品
    @Override
    public List<Cart> findList() {
        //1. 构建条件查询 select * from shopping_cate where user_id=?
        LambdaQueryWrapper<Cart> qw = new LambdaQueryWrapper<>();
        //冲线程中获取登录用户id==UserHolder.get()
        qw.eq(Cart::getUserId, UserHolder.get().getId());
        //执行查询并返回
        return cartMapper.selectList(qw);

    }
//修改购物车
    @Override
    public Cart update(Cart cart) {
        //查询需要修改的购物车对象
        LambdaQueryWrapper<Cart> qw = new LambdaQueryWrapper<>();
        //根据用户id
        qw.eq( cart.getUserId()!=null,Cart::getUserId, UserHolder.get().getId());
        //根据dish_id
        qw.eq(cart.getDishId() != null,Cart::getDishId, cart.getDishId());
        //根据setmeal_id
        qw.eq(cart.getSetmealId()!=null,Cart::getSetmealId, cart.getSetmealId());

        Cart carts = cartMapper.selectOne(qw);
        //判断修改之后的数量

        carts.setNumber(carts.getNumber()-1);
        if (carts.getNumber() > 0) {
        //操作用户减一
            cartMapper.updateById(carts);
        } else if (carts.getNumber() == 0) {

            cartMapper.deleteById(carts);

        }
        //返回结果
        return carts;

    }
//清空购物车
    @Override
    public void clean() {
        //构建条件 delete from shopping_cart where user_id =?
        LambdaQueryWrapper<Cart> qw = new LambdaQueryWrapper<>();
        qw.eq(Cart::getUserId, UserHolder.get().getId());
        //执行删除
        cartMapper.delete(qw);

    }
}
